// pages/totalRanking/totalRanking.js
import Toast from '@vant/weapp/toast/toast'
import {request} from '../../utils/request.js'
Page({
  subChange(event) {//科目变化
    this.checkSub = event.detail
    this.setData({
      checkSub: event.detail
    })
    this.optionInit()
    this.findTotalScoreRankList()
  },
  radioRunkChange(event) {//排序变化
    this.radioRunk = event.detail
    this.setData({
      radioRunk: event.detail
    })
    this.optionInit()
    this.findTotalScoreRankList()
  },
  classChange(event){//班级变化
    this.checkClass = event.detail
    this.setData({
      checkClass: event.detail
    })
    this.optionInit()
    this.findTotalScoreRankList()
  },
  //考试信息
  findExamById(){
    let vm = this
    request({
      url:'/review-paper/exam/findExamById',
      data:{
        id: vm.examId
      },
      success(res){
        if(res.data.status == 1){
          //选中所有科目
          vm.checkSub = []
          res.data.data.subjectExamList.forEach(val => {
            vm.checkSub.push(val.id)
          })
          vm.subArray = res.data.data.subjectExamList
          vm.setData({
            checkSub: vm.checkSub,
            subArray: vm.subArray,
          })
          vm.findClassList()
        }else{
          Toast.fail(res.data.msg);
        }
      }
    })
  },
  //班级
  findClassList(){
    let vm = this
    request({
      url:'/review-paper/exam/findClassList',
      data:{
        schoolId: vm.data.schoolInfo.id,
        examId: vm.examId
      },
      success(res){
        if(res.data.status == 1){
          //选中所有班级
          vm.checkClass = []
          res.data.data.forEach(val => {
            vm.checkClass.push(val.id)
          })
          vm.classArray = res.data.data
          vm.setData({
            checkClass:vm.checkClass,
            classArray: vm.classArray
          })
          //排行榜表格头部
          vm.optionInit()
          vm.findTotalScoreRankList()
        }else{
          Toast.fail(res.data.msg);
        }
      }
    })
  },
  //排行榜
  findTotalScoreRankList(){
    let vm = this
    request({
      url:'/review-paper/statistics/findTotalScoreRankList',
      data:{
        examId:vm.examId,
        subjectExamIds: vm.checkSub,
        classIds:vm.checkClass.join(',')
      },
      success (res) {
        if(res.data.status == 1){
          res.data.data.forEach((val) => {
            if(val.studentAnswerList){
              val.studentAnswerList.forEach((v) =>{
                  val['topicType' + v.topicNumber] = v.topicType
                  val['topicScore' + v.topicNumber] = v.topicScore
                  val['answer' + v.topicNumber] = v.answer
              })
            }
            //按题块分析
            if(val.mainNumberScoreList){
              val.mainNumberScoreList.forEach((v) =>{
                  if(v.mainNumber == '客观题'){
                      val['topicScore'] = v.topicScore
                  }else{
                      val[v.mainNumber + 'topicScore'] = v.topicScore
                  }
              })
            }
          })
          vm.list = res.data.data
          vm.setData({
            list:vm.list
          })
        }else{
          Toast.fail(res.data.msg);
        }
      }
    })
  },
  optionInit(){//排行榜配置初始化
    let arr = [
      { title: '总排名', width:"60", key: 'schoolRank' },
      { title: '姓名',  width:"80",key: 'realName' },
      { title: '总分',  width:"45", key: 'totalScore' },
      { title: '班级', width:"55", key: 'className' },
      { title: '班级排名', width:"75", key: 'classRank' },
      { title: '学号',  width:"75", key: 'studentNumber' }]
      function titleInit (it){
        if (it.topicType == '1' || it.topicType == '2' || it.topicType == '8'){
            return '客-' + it.topicNumber + '（满分：' + it.topicScore + '）'
        }
        return '主-' + it.topicNumber + '（满分：' + it.topicScore + '）'
      }
      if(this.data.radioRunk == '小题'){
        this.subArray.forEach(val => {
          if(val.id == this.data.checkSub){
            val.topicList.forEach(va=>{
              let obj = {
                  title:titleInit(va),
                  width:'120',
                  children:[]
                }
                if(va.topicType==1||va.topicType==2||va.topicType==8){
                  obj.children=[
                    {title:'答案'+va.answer||'-',key:'answer' + va.topicNumber,width:'60'},
                    {title:'得分',key:'topicScore' + va.topicNumber,width:'61'},
                  ]
                }else{
                  obj.children=[
                    {title:'得分',key:'topicScore' + va.topicNumber,width:'120'},
                  ]
                }
              arr.push(obj)
            })
          }
        })
      }else{//题块
        this.subArray.forEach(val => {
          if(val.id == this.data.checkSub){
            val.mainNumberList.forEach(va =>{
              arr.push({
                title:va.mainNumber=='客观题'?'客观题':'题块：'+va.mainNumber,
                key:va.mainNumber=='客观题'?'topicScore':va.mainNumber+'topicScore',
                width:'120'
              })
            })
          }
        })
      }
    this.setData({
      columns: arr
    })
  },
  data: {
    examId:'',
    checkClass:[],//班级
    checkSub:[],//科目
    radioRunk:'小题',
    runkList:[{ label: '小题' }, { label: '题块' }],//排列方式
    subArray:[],//科目列表
    classArray:[],//班级列表
    option: {},
    list:[],
    columns:[],//表头
    tableData:[]//表内容
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option) {
    this.examId = option.id
    
    let vm = this
    wx.getStorage({
      key:'schoolInfo',
      success(res){
        vm.setData({
          schoolInfo: res.data || {},
        })
        
        vm.findExamById()
      }
    })
  },
})